using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using umbraco.cms.businesslogic.web;
using umbraco.cms.businesslogic.property;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.BusinessLogic.console;
using System.Xml;
using System.IO;

namespace TG.Umb.DateFolder
{
    public class DateFolder_ActionHandler: umbraco.BusinessLogic.Actions.IActionHandler
    {

        string umbraco.BusinessLogic.Actions.IActionHandler.HandlerName()
        {
            return "TG.Umb.DateFolder.DateFolder_ActionHandler";
        }

        umbraco.interfaces.IAction[] umbraco.BusinessLogic.Actions.IActionHandler.ReturnActions()
        {
            return new umbraco.interfaces.IAction[] { new umbraco.BusinessLogic.Actions.ActionNew() };
        }

         Boolean umbraco.BusinessLogic.Actions.IActionHandler.Execute(umbraco.cms.businesslogic.web.Document documentObject, umbraco.interfaces.IAction action)
        {


            if (!isValid(documentObject.ContentType.Alias))
            {
                return false;
            }

            if (new Document(documentObject.Parent.Id).ContentType.Alias != "DateFolder")
            {

               

                string[] strArray = (documentObject.CreateDateTime.Year.ToString() + "-" + documentObject.CreateDateTime.Month.ToString() + "-" + documentObject.CreateDateTime.Day.ToString()).Split("-".ToCharArray());
                if (strArray.Length == 3)
                {
                    Document document = null;
                    foreach (IconI ni in documentObject.Parent.Children)
                    {
                        if (ni.Text == strArray[0])
                        {
                            document = new Document(ni.Id);
                            break;
                        }
                    }
                    if (document == null)
                    {
                        document = Document.MakeNew(strArray[0], DocumentType.GetByAlias("DateFolder"), documentObject.User, documentObject.Parent.Id);
                        document.Publish(documentObject.User);
                        library.PublishSingleNode(document.Id);
                    }
                    else
                    {

                        Log.Add(LogTypes.Debug, documentObject.User, documentObject.Id, "executed - year not null");
                    }
                    Document document2 = null;
                    Document[] children = document.Children;
                    for (int i = 0; i < children.Length; i++)
                    {
                        IconI ni2 = children[i];
                        if (ni2.Text == strArray[1])
                        {
                            document2 = new Document(ni2.Id);
                            break;
                        }
                    }
                    if (document2 == null)
                    {
                        document2 = Document.MakeNew(strArray[1], DocumentType.GetByAlias("DateFolder"), documentObject.User, document.Id);
                        document2.Publish(documentObject.User);
                        library.PublishSingleNode(document2.Id);
                    }
                    Document document3 = null;
                    Document[] documentArray2 = document2.Children;
                    for (int j = 0; j < documentArray2.Length; j++)
                    {
                        IconI ni3 = documentArray2[j];
                        if (ni3.Text == strArray[2])
                        {
                            document3 = new Document(ni3.Id);
                            break;
                        }
                    }
                    if (document3 == null)
                    {
                        document3 = Document.MakeNew(strArray[2], DocumentType.GetByAlias("DateFolder"), documentObject.User, document2.Id);
                        document3.Publish(documentObject.User);
                        library.PublishSingleNode(document3.Id);
                    }
                    documentObject.Move(document3.Id);

                }
            }
            return true;

        

           

        }

        private bool isValid(string Alias)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(HttpContext.Current.Server.MapPath("/config/TG.Umb.DateFolder/DateFolder.config"));

            foreach (XmlNode DocumentTypeAlias in xd.SelectNodes("DateFolder/DocumentTypeAlias"))
            {
               

                if (DocumentTypeAlias.InnerText.Trim() == Alias)
                {
                    

                    return true;


                }
            }

            return false;

        }


    }
}
